package com.example.base.tools.map

import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.OnLifecycleEvent


/**  地理位置 POI热词建议检索、在线建议检索 工具类  **/
@Suppress("unused")
object LocationSuggestionTool {

   /* *//**  是否已经进行过初始化  **//*
    private var isInit = false

    *//**  生命周期的监听类, 自动进行销毁操作  **//*
    private class SuggestionLifecycleListener : LifecycleObserver {
        @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
        fun destroySuggestion() {
            suggestionSearch.destroy()
            isInit = false
        }
    }

    *//**  POI热词建议检索、在线建议检索 实例  **//*
    private val suggestionSearch: SuggestionSearch by lazy {
        SuggestionSearch.newInstance()
    }

    *//**  检索结果监听器  **//*
    private val listener: OnGetSuggestionResultListener by lazy {
        OnGetSuggestionResultListener { suggestionResult ->

            // 过滤掉空结果和无经纬度的结果, 以免导致闪退
            val originalResult = suggestionResult.allSuggestions
            val newResult = ArrayList<SuggestionResult.SuggestionInfo>()
            originalResult?.forEach { suggestionInfo ->
                if (suggestionInfo != null)
                    if (suggestionInfo.pt != null)
                        newResult.add(suggestionInfo)
            }

            onSuggestionResultListener?.onGetSuggestionResult(newResult)
        }
    }

    *//**  检索结果回调监听器  **//*
    private var onSuggestionResultListener: OnSuggestionResultListener? = null

    *//**  初始化工具类  **//*
    @Synchronized
    fun init(lifecycleOwner: LifecycleOwner, suggestionListener: OnSuggestionResultListener) {
        synchronized(LocationSuggestionTool::class.java) {
            onSuggestionResultListener = suggestionListener
            lifecycleOwner.lifecycle.addObserver(SuggestionLifecycleListener())
            suggestionSearch.setOnGetSuggestionResultListener(listener)
            isInit = true
        }
    }

    *//**
     * 根据条件获取检索结果
     * @param cityName 城市名称
     * @param keyword 关键词
     *//*
    fun requestSuggestion(cityName: String, keyword: String) {
        suggestionSearch.requestSuggestion(SuggestionSearchOption().city(cityName).keyword(keyword))
    }

    *//**  检索结果监听接口  **//*
    interface OnSuggestionResultListener {
        *//**
         * 当获取检索结果成功时回调
         * @param result 检索的结果列表
         *//*
        fun onGetSuggestionResult(result: List<SuggestionResult.SuggestionInfo>?)

        *//**  当获取检索结果失败时回调  **//*
        fun onGetSuggestionError()
    }*/

}